package org.liwuyi.demo.service.impl;

import jakarta.annotation.Resource;
import org.liwuyi.demo.entity.Personnel;
import org.liwuyi.demo.mapper.PersonnelMapper;
import org.liwuyi.demo.service.PersonnelService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 人员信息Service实现类
 * @author Ainsley
 */
@Service
public class PersonnelServiceImpl implements PersonnelService {

    @Resource
    private PersonnelMapper personnelMapper;

    // 存储上一次查询的personId集合
    private Set<Long> lastPersonIds = new HashSet<>();
    
    // 存储新增的personId
    private final CopyOnWriteArrayList<Long> addedIds = new CopyOnWriteArrayList<>();
    
    // 存储删除的personId
    private final CopyOnWriteArrayList<Long> removedIds = new CopyOnWriteArrayList<>();

    /**
     * 批量添加人员信息
     * @param personnelList 人员信息列表
     * @return 结果
     */
    @Override
    public int batchAddPersonnel(List<Personnel> personnelList) {
        System.out.println("进入批量添加人员信息方法");
        System.out.println(personnelList);
        return personnelMapper.batchInsertPersonnel(personnelList);
    }
    
    /**
     * 查询所有人员ID
     * @return 人员ID集合
     */
    public Set<Long> queryAllPersonIds() {
        try {
            return personnelMapper.queryAllPersonIds();
        } catch (Exception e) {
            System.out.println("查询所有人员ID失败: " + e.getMessage());
            return new HashSet<>();
        }
    }
    
    /**
     * 每10秒执行一次，监控数据变更
     */
    @Scheduled(fixedRate = 10000)
    public void monitorDataChanges() {
        // 查询当前数据库中的所有personId
        Set<Long> currentPersonIds = queryAllPersonIds();
        
        // 如果是首次执行，初始化lastPersonIds并返回
        if (lastPersonIds.isEmpty()) {
            lastPersonIds = new HashSet<>(currentPersonIds);
            System.out.println("数据变更监控服务已启动，当前总记录数：" + currentPersonIds.size());
            return;
        }
        
        // 清空上一次的变更记录
        addedIds.clear();
        removedIds.clear();
        
        // 查找新增的数据ID
        for (Long id : currentPersonIds) {
            if (!lastPersonIds.contains(id)) {
                addedIds.add(id);
            }
        }
        
        // 查找删除的数据ID
        for (Long id : lastPersonIds) {
            if (!currentPersonIds.contains(id)) {
                removedIds.add(id);
            }
        }
        
        // 更新lastPersonIds
        lastPersonIds = new HashSet<>(currentPersonIds);
        
        // 输出变更信息
        if (!addedIds.isEmpty() || !removedIds.isEmpty()) {
            System.out.println("======= 数据变更通知 =======");
            if (!addedIds.isEmpty()) {
                System.out.println("新增数据ID: " + addedIds);
            }
            if (!removedIds.isEmpty()) {
                System.out.println("删除数据ID: " + removedIds);
            }
            System.out.println("===========================");
        }
    }
    
    /**
     * 获取新增的数据ID
     * @return 新增数据ID列表
     */
    public CopyOnWriteArrayList<Long> getAddedIds() {
        return addedIds;
    }
    
    /**
     * 获取删除的数据ID
     * @return 删除数据ID列表
     */
    public CopyOnWriteArrayList<Long> getRemovedIds() {
        return removedIds;
    }
}